from django.shortcuts import render
from django.views.generic import View, ListView, TemplateView
from resources.models import *
from django.http.response import JsonResponse


# 通过传入的请求获取 组合成字典数据
def get_dict(request):
    dict = {}
    for one in request.POST.items():
        if one[0] != 'csrfmiddlewaretoken':
            dict[one[0]] = one[1]
    return dict


# Create your views here.
class IdcListView(ListView):
    template_name = 'idc_list.html'
    model = Idc

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['username'] = str(self.request.user)
        # print(self.request.user)
        # print(type(self.request.user))
        return context


class IdcAddView(TemplateView):
    template_name = 'idc_add.html'

    def post(self, request):
        res = {'status': 0, 'msg': '添加机房成功'}
        try:
            Idc.objects.create(**get_dict(request))
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '添加机房失败'}
        return JsonResponse(res)


class IdcUpdateView(TemplateView):
    template_name = 'idc_update.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['idc_obj'] = Idc.objects.get(id=self.request.GET.get('id'))
        return context

    def post(self, request):
        # print(request.POST)
        res = {'status': 0, 'msg': '更新主机信息成功'}
        try:
            # 更新操作先要找到需要修改的对象
            idc = Idc.objects.get(id=request.POST.get('id'))
            idc.name = request.POST.get('name')
            idc.name_cn = request.POST.get('name_cn')
            idc.address = request.POST.get('address')
            idc.phone = request.POST.get('phone')
            idc.username = request.POST.get('username')
            idc.user_email = request.POST.get('user_email')
            idc.user_phone = request.POST.get('user_phone')
            idc.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '更新主机信息失败'}
        return JsonResponse(res)


class IdcDeleteView(View):
    def post(self, request):
        # print(request.POST)
        res = {'status': 0, 'msg': '删除机房成功'}
        try:
            # pk 主键  默认模型创建了id字段为主键
            Idc.objects.get(pk=request.POST.get('id')).delete()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '删除机房失败'}
        return JsonResponse(res)


class ServerUserListView(ListView):
    template_name = 'serveruser_list.html'
    model = ServerUser


class ServerUserAddView(TemplateView):
    template_name = 'serveruser_add.html'

    def post(self, request):
        res = {'status': 0, 'msg': '添加资产用户成功'}
        try:
            serveruser = ServerUser()
            serveruser.name = request.POST.get('name')
            serveruser.username = request.POST.get('username')
            serveruser.password = request.POST.get('password')
            serveruser.info = request.POST.get('info')
            serveruser.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '添加资产用户失败'}
        return JsonResponse(res)


class ServerUserUpdateView(View):
    def get(self, request):
        serveruser_obj = ServerUser.objects.get(id=request.GET.get('id'))
        return render(request, 'serveruser_update.html', locals())

    def post(self, request):
        res = {'status': 0, 'msg': '更新资产用户成功'}
        try:
            serveruser = ServerUser.objects.get(id=request.POST.get('id'))
            serveruser.name = request.POST.get('name')
            serveruser.username = request.POST.get('username')
            serveruser.password = request.POST.get('password')
            serveruser.info = request.POST.get('info')
            serveruser.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '更新资产用户失败'}
        return JsonResponse(res)


class ServerUserDeleteView(View):
    def post(self, request):
        # print(request.POST)
        res = {'status': 0, 'msg': '删除资产用户成功'}
        try:
            # 1.这步是什么操作
            ServerUser.objects.get(id=request.POST.get('id')).delete()
        except Exception as e:
            # 2.e是什么？
            print(e)
            res = {'status': 1, 'msg': '删除资产用户失败'}
            # 3.响应信息给前端页面返回什么格式数据
        return JsonResponse(res)


class ServerListView(ListView):
    template_name = 'server_list.html'
    model = Server


class ServerDataApiView(View):
    def post(self,request):
        # 客户端会通过requests库发送post请求
        print(request.body)
        res = {'status':0,'msg':'ok'}
        return JsonResponse(res)